/**  
 * @Title: ReceiveFileTest.java
 * @Package com.icss.trans.ws.push
 * @Description: TODO(用一句话描述该文件做什么)
 * @author ICSS
 * @date 2015-3-30 下午6:00:15
 * @version V1.0  
 */ 
package com.icss.trans.ws.push;

import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.junit.Test;


/**
 * @ClassName: ReceiveFileTest
 * @Description: 
 * @author ICSS
 * @date 2015-3-30 下午6:00:15
 *
 */
public class ReceiveFileTest {

    /**
     * Test method for {@link com.icssi.webservice.receive.ReceiveFile#ReceiveFile(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)}.
     */
    @Test
    public void testReceiveFile() {
        try {
            String result= testOtherWebservice("080001", "10000001", "030001", "080001002", "1", "1", "6F7E617ECBA2C1FA3EBE2DFAE8BECBC7");
            System.out.println(result);
            } catch (Exception e) {
             e.printStackTrace();
            }
    }


    // 名字空间（不可变）
    public static final String targetNamespace = "http://receive.webservice.icssi.com/";
    // 服务名（不可变）
    public static final String serName = "ReceiveFileService";
    // 端口名（不可变）
    public static final String pName = "ReceiveFilePort";
    //第三方地址（ip和端口第三方提供）
    public static final String URL="http://192.168.20.87:8080/";
    // 服务地址（不可变）
    public static final String endpointAddress = "dtcp/ReceiveFileService?wsdl";
    // 方法名（不可变）
    public static final String OPER_NAME = "ReceiveFile";
    /**
     * 
     * @Description: 
     * @param sourceAppCode 源节点组织编码
     * @param sourceOrgCode源应用编码
     * @param destAppCode 目标应用编码
     * @param dataTransType 数据传输类型
     * @param fileContent 字符串内容
     * @param fileName 文件名称
     * @param sysTransId  传输唯一标识
     * @return
     * @throws Exception
     */
    public static String testOtherWebservice(String sourceAppCode, 
            String sourceOrgCode, String destAppCode, String dataTransType, 
            String fileContent, String fileName, String sysTransId) throws Exception {
            QName serviceName = new QName(targetNamespace, serName);
    
            QName portName = new QName(targetNamespace, pName);
    
            javax.xml.ws.Service service = Service.create(serviceName);
            service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING,URL+endpointAddress );
    
            Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
    
            BindingProvider bp = (BindingProvider) dispatch;
            Map<String, Object> rc = bp.getRequestContext();
            rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
            rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, OPER_NAME);
            MessageFactory factory = ((SOAPBinding) bp.getBinding()).getMessageFactory();
    
            SOAPMessage request = factory.createMessage();
            SOAPBody body = request.getSOAPBody();
            QName payloadName = new QName(targetNamespace, OPER_NAME, "nsl");
            SOAPBodyElement payload = body.addBodyElement(payloadName);
            //参数设置
            SOAPElement message2 = payload.addChildElement("sysTransId");
            message2.addTextNode(sysTransId);
            SOAPElement message3 = payload.addChildElement("sourceOrgCode");
            message3.addTextNode(sourceOrgCode);
            SOAPElement message4 = payload.addChildElement("sourceAppCode");
            message4.addTextNode(sourceAppCode);
            SOAPElement message6 = payload.addChildElement("destAppCode");
            message6.addTextNode(destAppCode);
            SOAPElement message7 = payload.addChildElement("dataTransType");
            message7.addTextNode(dataTransType);
            SOAPElement message8 = payload.addChildElement("fileName");
            message8.addTextNode(fileName);
            SOAPElement message9 = payload.addChildElement("fileContent");
            message9.addTextNode(fileContent);
            
            SOAPMessage reply = null;
            reply = dispatch.invoke(request);
            
            SOAPBody soapBody = reply.getSOAPBody();
            SOAPBodyElement nextSoapBodyElement = (SOAPBodyElement) soapBody.getChildElements().next();
            SOAPElement soapElement = (SOAPElement) nextSoapBodyElement.getChildElements().next();
    
            return soapElement.getValue();
    }


}
